package com.example.thread.d9CompletableFuture2.d9大数据商品比价;

import java.util.Arrays;
import java.util.List;

public class ComparePriceDemo {
    public static void main(String[] args) {
        // 方案一，使用串行操作商品价格
//        ComparePriceService service = new ComparePriceService();
//        long start = System.currentTimeMillis();
//        PriceResult price = service.getCheapestPlatformPrice("iphone14");
//        long end = System.currentTimeMillis();
//        double coust = (end - start) / 1000.0;
//        System.out.printf("运行花费时间 %.2f\n", coust);
//        System.out.println(price);

        // 方案二，使用Future+线程池增强并行,Future+线程池 提高了任务处理的并行性
//        ComparePriceService service = new ComparePriceService();
//        long start = System.currentTimeMillis();
//        PriceResult price = service.getCheapestPlatformPrice2("iphone14");
//        long end = System.currentTimeMillis();
//        double coust = (end - start) / 1000.0;
//        System.out.printf("运行花费时间 %.2f\n", coust);
//        System.out.println(price);

        // 方案三，使用CompletableFuture进一步增强
//        ComparePriceService service = new ComparePriceService();
//        long start = System.currentTimeMillis();
//        PriceResult price = service.getCheapestPlatformPrice3("iphone14");
//        long end = System.currentTimeMillis();
//        double coust = (end - start) / 1000.0;
//        System.out.printf("运行花费时间 %.2f\n", coust);
//        System.out.println(price);

        // 异步任务的批量操作
        List<String> products = Arrays.asList("iphone13", "iphone23", "iphone22");
        ComparePriceService service = new ComparePriceService();
        PriceResult priceResult = service.batchComparePrice(products);
        System.out.println(priceResult);

    }
}
